﻿using System;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Negocio;

namespace Persistencia
{
    class BrokerReferenceMeasure : Broker
    {
        public BrokerReferenceMeasure()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            ReferenceMeasure measure = (ReferenceMeasure)o;
            this.conn.connect();
            string sql = "insert into ReferenceMeasure (oid,name,value_) values(" + measure.OID + ",'" + measure.Name + "'," + measure.Value + ")";
            this.conn.execute(sql);
            string sql1 = "update OID set OId=" + measure.OID;
            this.conn.execute(sql1);
        }

        public override void update(Object o)
        {
            ReferenceMeasure measure = (ReferenceMeasure)o;
            this.conn.connect();
           string sql = "update ReferenceMeasure set name='" + measure.Name + "',value_ = " + measure.Value+ "' where oid = " + measure.OID;
            string sql1 = "update OID set OId=" + measure.OID;
            this.conn.execute(sql);
            this.conn.execute(sql1);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            ReferenceMeasure measure = (ReferenceMeasure)o;
            //this.conn.connect();
            string sql = "delete from ReferenceMeasure where oid = " + measure.OID;
            this.conn.execute(sql);
            //this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            ReferenceMeasure measure = (ReferenceMeasure)o;
            this.conn.connect();
            string sql = "Select * from ReferenceMeasure where oid =" + measure.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                measure.Name = (String)this.conn.getDataString("name");
                measure.Value = float.Parse(this.conn.getDataString("value_").ToString());
                measure.OID = int.Parse(this.conn.getDataString("OId").ToString());
             }
            this.conn.disconnect();

        }

        public override ObservableCollection<Object> bringList(Object o)
        {
            string sql = "Select * from ReferenceMeasure";
            ObservableCollection<Object> lista = new ObservableCollection<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                ReferenceMeasure measure = new ReferenceMeasure();
                measure.Name = this.conn.getDataString("name").ToString();
                measure.Value = float.Parse(this.conn.getDataString("value_").ToString());
                measure.OID = int.Parse(this.conn.getDataString("Oid").ToString());
                
                lista.Add(measure);
            }
            this.conn.disconnect();
            return lista;
        }
    }
}